﻿using System;
using System.IO;
using Common.Utility;
using Prism.Mvvm;
using UsingEventAggregator.Core;
using HPIO = Hprose.IO;
using Prism.Events;

namespace DataDefine
{
    [Serializable]
    public enum AccessLv
    {
        /// <summary>
        /// 用户
        /// </summary>
        User,
        /// <summary>
        /// 工程师
        /// </summary>
        Engineer,
        /// <summary>
        /// 厂家
        /// </summary>
        OEM
    }

    [Serializable]
    public enum VType
    {
        Ushort,
        Short,
        Byte,
        Bool,
        Float1,
        Float2,
        Float3,
        HMI_Ushort,
        HMI_Short,
        HMI_Byte,
        HMI_Bool,
        HMI_Float1,
        HMI_Float2,
        HMI_Float3,
    }

    [Serializable]
    public enum VRWType
    {
        ReadWrite =0,
        ReadOnly,
        /// <summary>
        /// 被动只读
        /// </summary>
        PassivityReadOnly
    }

 
 
    /// <summary>
    /// BinaryFormatter bf = new BinaryFormatter();
    /// [Serializable]
    /// NonSerialized
    /// </summary>
    [Serializable]
    public class SFMDocument : BindableBase
    {
        private string _SFMVersion = "V1.0";
        public string SFMVersion
        {
            get { return _SFMVersion; }
            set { _SFMVersion = value; }
        }

        private string _System="";
        public string System
        {
            get { return _System; }
            set { _System = value; }
        }

        public Device[] _Devices;
        public Device[] Devices
        {
            get { return _Devices; }
            set
            {
                SetProperty(ref _Devices, value);
            }
        }

        private string FileName;

        [NonSerialized]
        private bool _CanSave;

        public bool CanSave
        {
            get { return _CanSave; }
            set { _CanSave = value; }
        }

        public void Save()
        {
            if (File.Exists(FileName))
            {
                SaveAs(FileName, this);
            }
        }

        //密钥
        private static string arrDESKey = "42, 16, 93, 98, 125, 156, 78, 254, 18, 32";
        private static byte[] arrDESKeyArrray = { 42, 16, 93, 98, 125, 156, 78, 254, 18, 32 };

        /// <summary>
        /// 1. Serialize
        /// 2. Encrypt
        /// 3. Write
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="data"></param>
        public static void SaveAs(string FileName, SFMDocument data)
        {
            HPIO.Serializer serializer = new HPIO.Serializer();
            FileStream sr = (File.OpenWrite(FileName));
            MemoryStream ms = new MemoryStream();
            serializer.Serialize(new HPIO.Writer(ms), data);
            byte[] strbytes = DEncrypt.Encrypt(ms.ToArray(), arrDESKeyArrray);
            sr.Write(strbytes, 0, strbytes.Length);//
            sr.Close();
        }

        /// <summary>
        /// 1. Serialize
        /// 2. Encrypt
        /// 3. Write
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="data"></param>
        private static void SaveAs2(string FileName, SFMDocument data)
        {
            HPIO.Serializer serializer = new HPIO.Serializer();
            MemoryStream ms = new MemoryStream();
            serializer.Serialize(new HPIO.Writer(ms), data);
        }


        /// <summary>
        /// 1. Read
        /// 2. Decrypt
        /// 3. Deserializer
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static SFMDocument Load(string FileName)
        {
            if (File.Exists(FileName))
            {
                SFMDocument f1 = null;
                try
                {
                    //TODO:不调用会引发异常
                    SaveAs2("FFF", new SFMDocument());

                    MemoryStream ms = new MemoryStream();
                    FileStream sr = (File.OpenRead(FileName));
                    sr.CopyTo(ms);
                    byte[] data2 = DEncrypt.Decrypt(ms.ToArray(), arrDESKeyArrray);
                    MemoryStream ms2 = new MemoryStream(data2);
                    HPIO.Deserializer deserializer = new HPIO.Deserializer();
                    var obj = deserializer.Deserialize(new HPIO.Reader(ms2));

                    f1 = (SFMDocument)obj;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return null;
                }

                if (f1 != null)
                {
                    f1.FileName = FileName;
                    f1.CanSave = true;
                    return f1;
                }
                return null;
            }
            return null;
        }

        private static SFMDocument sfm;

        public static SFMDocument Create()
        {
            if (sfm == null)
            {
                sfm = new SFMDocument();
                sfm.System = " ";
                sfm.Devices = new Device[0];
                return sfm;
            }
            return sfm;
        }

        public static SFMDocument CreateRandom()
        {
            sfm = new SFMDocument();
            sfm.System = "TLD整车系统";

            sfm.Devices = new Device[10];//10个设备
            for (int i = 0; i < sfm.Devices.Length; i++)
            {
                Device dev = new Device();
                dev.DeviceInfo = new DeviceInfo();
                dev.AdditionalInfo = new AdditInfo();
                dev.Parameters = new RunParameter[10];
                dev.DeviceInfo.DeviceName = "Name " + i;
                dev.DeviceInfo.DeviceType = "Car";
                dev.DeviceInfo.HWVersion = "A03-" + i;
                dev.DeviceInfo.HWNumber = "000001" + i;
                dev.DeviceInfo.SWAppVersion = "R3.04";
                dev.DeviceInfo.SWBootloaderVersion = "V0.1";
                dev.DeviceInfo.CANNode_ID = "235." + i;
                dev.DeviceInfo.Parent = dev;
                dev.AdditionalInfo.ShippingInfo = new ShippingInfo();
                dev.AdditionalInfo.ShippingInfo.ShippingDate = DateTime.Now;
                dev.AdditionalInfo.ShippingInfo.OrderNumber = "DD-200514-" + i;
                dev.AdditionalInfo.Notes = "";

                RunParameter par = new RunParameter();
                par.ParameterName = "Par_" + i;
                par.Value = 1;
                par.Min = 0;
                par.Max = 1;
                par.Default = 0;
                par.Unit = "";
                par.ReadAccessLv = AccessLv.User;
                par.WriteAccessLv = AccessLv.User;
                par.Type = VType.Bool;
                dev.Parameters[0] = par;

                Random r = new Random();
                for (int j = 1; j < dev.Parameters.Length; j++)
                {
                    RunParameter par2 = new RunParameter();
                    par2.ParameterName = "Par_" + i + "." + j;
                    par2.Value = r.Next(0, 1234567);
                    par2.Min = r.Next(-1, 0);
                    par2.Max = r.Next(0, 1234567);
                    par2.Default = 0;
                    par2.Unit = "";
                    par2.ReadAccessLv = AccessLv.User;
                    par2.WriteAccessLv = AccessLv.User;
                    par2.Type = VType.Ushort;
                    dev.Parameters[j] = par2;
                }
                sfm.Devices[i] = dev;
            }

            return sfm;
        }

        public static SFMDocument CreateZero(int DevCount, int[] ParCount)
        {
            SFMDocument tmp = new SFMDocument();
            tmp.System = "TLD整车系统";
            tmp.Devices = new Device[DevCount];
            for (int i = 0; i < tmp.Devices.Length; i++)
            {
                Device dev = new Device();
                dev.DeviceInfo = new DeviceInfo();
                dev.AdditionalInfo = new AdditInfo();
                dev.Parameters = new RunParameter[ParCount[i]];
                dev.DeviceInfo.DeviceName = "Name " + i;
                dev.DeviceInfo.DeviceType = "Car";
                dev.DeviceInfo.HWVersion = "A03-" + i;
                dev.DeviceInfo.HWNumber = "000001" + i;
                dev.DeviceInfo.SWAppVersion = "R3.04";
                dev.DeviceInfo.SWBootloaderVersion = "V0.1";
                dev.DeviceInfo.CANNode_ID = "235." + i;
                dev.DeviceInfo.Parent = dev;
                dev.AdditionalInfo.ShippingInfo = new ShippingInfo();
                dev.AdditionalInfo.ShippingInfo.ShippingDate = DateTime.Now;
                dev.AdditionalInfo.ShippingInfo.OrderNumber = "DD-200514-" + i;
                dev.AdditionalInfo.Notes = "";
                for (int j = 0; j < dev.Parameters.Length; j++)
                {
                    RunParameter par2 = new RunParameter();
                    par2.ParameterName = "Par_" + i + "." + j;
                    par2.Value = 0;
                    par2.Min = 0;
                    par2.Max = 99999;
                    par2.Default = 0;
                    par2.Unit = "";
                    par2.ReadAccessLv = AccessLv.User;
                    par2.WriteAccessLv = AccessLv.User;
                    par2.Type = VType.Ushort;
                    dev.Parameters[j] = par2;
                }
                tmp.Devices[i] = dev;
            }
            return tmp;
        }

        public static void SetSFMDocument(SFMDocument doc)
        {
            sfm = doc;
        }
    }
}



